package main

import (
	"bufio"
	"fmt"
	"os"
)

func tryDefer() {
	for i := 0; i <= 30; i++ {
		defer println(i)
		if i == 30 {
			panic("打印次数超过了 30 次")
		}
	}
}

func writeFile(fileName string) {
	file, err := os.OpenFile(fileName, os.O_EXCL|os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		pathError, ok := err.(*os.PathError)
		if !ok {
			panic(err)
		} else {
			fmt.Printf("%s, %s, %s\n",
				pathError.Op,
				pathError.Path,
				pathError.Err)
		}
		return
	}
	defer file.Close()

	writer := bufio.NewWriter(file)

	defer writer.Flush()
	f := fib.Fibonacci()
	for i := 0; i < 20; i++ {
		fmt.Fprintln(writer, f())
	}

}
func main() {
	//tryDefer()
	writeFile("fib.txt")
}
